//
//  JipanViewController.m
//  Travel
//
//  Created by lanou3g on 15/4/10.
//  Copyright (c) 2015年 &#21556;&#21697;&#27901;. All rights reserved.
//

#import "JipanViewController.h"
#import <AFNetworking.h>
#import "Jipan.h"
#import "JipanCell.h"
#import <UIImageView+WebCache.h>
#import "ZhuantiViewController.h"
#import "YoujiViewController.h"
#import "XingchengViewController.h"
@interface JipanViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)NSMutableArray *arr;
@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,assign)NSInteger flag;
@end

@implementation JipanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    [self createrequest];
}
- (void) createrequest
{
    self.arr = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"https://chanyouji.com/api/destinations/%@.json?page=1",self.number];
    AFHTTPRequestOperationManager *mananger = [AFHTTPRequestOperationManager manager];
    [mananger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *array = responseObject;
        for (NSDictionary *dic in array) {
            Jipan *jp = [[Jipan alloc] init];
            [jp setValuesForKeysWithDictionary:dic];
            [self.arr addObject:jp];
            
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
}
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"jp";
    JipanCell *cell = [self.tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[JipanCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    Jipan *jp = [self.arr objectAtIndex:indexPath.row];
    
    NSURL *url = [NSURL URLWithString:jp.image_url];
    [cell.imageViewPhoto sd_setImageWithURL:url placeholderImage:nil];
    self.flag = indexPath.row;
    [cell.buttonZhuanti addTarget:self action:@selector(buttonZhuantiAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonyouji addTarget:self action:@selector(buttonyoujiAction:) forControlEvents:UIControlEventTouchUpInside];
    [cell.buttonWalk addTarget:self action:@selector(buttonWalkAction:) forControlEvents:UIControlEventTouchUpInside];
    if (indexPath.row == 0) {
        Jipan *jpA = [self.arr objectAtIndex:indexPath.row];
        NSString *strA = [NSString stringWithFormat:@"%@概览",jpA.name_zh_cn];
        cell.labelName.textAlignment = NSTextAlignmentLeft;
        cell.labelEngame.textAlignment = NSTextAlignmentLeft;
        cell.labelName.text = strA;
        cell.labelEngame.text = jpA.name_en;
    }
    else{
        cell.labelName.textAlignment = NSTextAlignmentLeft;
        cell.labelEngame.textAlignment = NSTextAlignmentLeft;
        cell.labelName.text = jp.name_zh_cn;
        cell.labelEngame.text = jp.name_en;
        
    }
    return cell;
}
- (void)buttonWalkAction:(UIButton *)button
{
    JipanCell * cell = (JipanCell *)[[button superview] superview];
    NSIndexPath * path = [self.tableView indexPathForCell:cell];
    NSLog(@"++++%ld",path.row);
    Jipan *jp = [self.arr objectAtIndex:path.row];
    XingchengViewController *xinVc = [[XingchengViewController alloc] init];
    xinVc.numberXingcheng = [jp.ID stringValue];
    xinVc.numberName = jp.name_zh_cn;
    [self.navigationController pushViewController:xinVc animated:YES];
    
}
- (void)buttonyoujiAction:(UIButton *)button
{
    JipanCell * cell = (JipanCell *)[[button superview] superview];
    NSIndexPath * path = [self.tableView indexPathForCell:cell];
    NSLog(@"++++%ld",path.row);
    Jipan *jp = [self.arr objectAtIndex:path.row];
    YoujiViewController *youjiVc = [[YoujiViewController alloc] init];
    youjiVc.numberYouji = [jp.ID stringValue];
    youjiVc.numberNameYouji = jp.name_zh_cn;
    [self.navigationController pushViewController:youjiVc animated:YES];
    

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"#######%ld",indexPath.row);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height /2.47;
}
- (void)buttonZhuantiAction:(UIButton *)button
{
    JipanCell * cell = (JipanCell *)[[button superview] superview];
    NSIndexPath * path = [self.tableView indexPathForCell:cell];
    NSLog(@"++++%ld",path.row);
    Jipan *jp = [self.arr objectAtIndex:path.row];
    ZhuantiViewController *zhuantiVc = [[ZhuantiViewController alloc] init];
    zhuantiVc.numberStr = [jp.ID stringValue];
    zhuantiVc.numberNameZhuanti = jp.name_zh_cn;
    [self.navigationController pushViewController:zhuantiVc animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
